using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace StudentManagementApplication
{
    public partial class FrmLogin : Form
    {
        public FrmMain frmMain = new FrmMain();
        Business.LoginService loginService = new Business.LoginService();
        Business.PersonService personService = new Business.PersonService();
        Entities.Login login = null;
        Entities.Person person = null;
        public FrmLogin()
        {
            InitializeComponent();
        }

        private void CheckUserAndPassword(string sUser, string sPassword)
        {
            login = loginService.GetAccountByLoginIDAndPassword(sUser, sPassword);
            if (login != null)
            {
                person = personService.GetPersonByLoginID(sUser);
                frmMain.sPersonID = person.PersonID;
                frmMain.sRole = login.RoleID;
                frmMain.sLoginID = login.LoginID;
                this.Close();
            }
            else
                MessageBox.Show("this account doesn't exist or password is wrong", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);

        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (txtUser.Text != string.Empty && txtPassword.Text != string.Empty)
                CheckUserAndPassword(txtUser.Text.ToLower().Trim(), txtPassword.Text.Trim());
            else
                MessageBox.Show("Please input user and password");
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.frmMain.Close();
            this.Close();
        }

        private void txtUser_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (txtUser.Text != string.Empty && txtPassword.Text != string.Empty)
                    CheckUserAndPassword(txtUser.Text.ToLower().Trim(), txtPassword.Text.Trim());
                else
                    MessageBox.Show("Please input user and password");
            }
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (txtUser.Text != string.Empty && txtPassword.Text != string.Empty)
                    CheckUserAndPassword(txtUser.Text.ToLower().Trim(), txtPassword.Text.Trim());
                else
                    MessageBox.Show("Please input user and password");
            }
        }
    }
}